<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\AdminModels\Admin;
use App\Models\AdminModels\AdminService;
use App\Models\AdminModels\AdminRandomUser;
use App\Models\UserModels\UserArea;
class AdminServiceController extends Controller{
  /**
   * 客户设置
   */
  public function index(){
    return view('login/admin-service/index',['admin_service_tree_menu'=>true]);
  }
  
  /**
   * 客户设置页面
   */
  public function userSet($id){
    $oAdminService = new AdminService();
    $oAdmin = new Admin();
    $result = $oAdminService->getAllUserByAdminId($id);
    $ids = !empty($result) ? array_column($result,'user_id') : [];
    $ids = implode(',', $ids);
    //用户地区信息
    $oArea = new UserArea();
    $area_list = $oArea->getCountryData();
    //是否是客服
    $adminData = $oAdmin->db_get(['admin_id'=>['=',$id]]);
    $admin_type = $adminData['type'];
    return view('login/admin-service/relation',['admin_service_tree_menu'=>true,'id'=>$id,'ids'=>$ids,'area_list'=>$area_list,'admin_type'=>$admin_type]);
  }
  
  /**
   * 我的客户
   */
  public function myService(Request $request){
    $session = $request->session()->get('user');
    $admin_id = $session[0]['admin_id'];
    return view('login/admin-service/service',['admin_my_service_tree_menu'=>true,'admin_id'=>$admin_id]);
  }

  /**
   * 随机客户
   */
  public function randomList(Request $request){
    $session = $request->session()->get('user');
    $admin_id = $session[0]['admin_id'];
    $oAdmin = new Admin();
    $adminData = $oAdmin->db_get(['admin_id'=>['=',$admin_id]]);
    $auth = 0;
    if(($adminData['role_id'] == 6 || $adminData['role_id'] == 30) && $adminData['type'] == 2){//如果角色是销售/销售团长，type也是销售
      $oAdminRandomUser = new AdminRandomUser();
      $oAdminRandomUser->randomUser($admin_id,2,$adminData['max_add_customer']);
      $auth = 1;
    }
    return view('login/admin-service/random',['admin_random_user_tree_menu'=>true,'admin_id'=>$admin_id,'auth' => $auth]);
  }

  /**
   * 查看我的客户
   */
  public function view($id,Request $request){
    $session = $request->session()->get('user');
    $admin_id = $session[0]['admin_id'];
    $oAdmin = new Admin();
    $adminData = $oAdmin->db_get(['admin_id'=>['=',$admin_id],'status'=>['>=',1]]);
    if(!empty($adminData)){
      if($adminData['role_id'] == 1){
        return redirect('admin/user/view/'.$id);
      }else{
        $oAdminService = new AdminService();
        $adminServiceData = $oAdminService->db_get(['admin_id'=>['=',$admin_id],'user_id'=>['=',$id]]);
        if(!empty($adminServiceData)){
          $oUser = new \App\Models\UserModels\User();
          $userData = $oUser->db_get(['user_id'=>['=',$id]]);
          if(!empty($userData)){
            $userData['user_source'] = \App\Components\Tools::userSource($userData['tid']);
            //用户信息
            $oUserInfo = new \App\Models\UserModels\UserInfo();
            $userInfo = $oUserInfo->db_get(['user_id'=>['=',$id]]);
            return view('login/admin-service/view', ['admin_my_service_tree_menu' => true,'data'=>$userData,'userInfo'=>$userInfo]);
          }
        }
      }
    }
    abort(403);
  }
}
